package com.xjd.edu.toolkit.metrics.config;

import com.xjd.edu.toolkit.metrics.feign.FeignClientBeanPostProcessor;
import feign.Client;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;

/**
 * Feign 相关指标配置
 *
 * @author wangyu
 * @since 2023/2/3
 */
@ConditionalOnClass(Client.class)
public class FeignMetricsConfiguration {

    // 官方实际有提供监控组件，基本符合当前需求，故删除原自定义feign监控组件
    // https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/#micrometer-support
    // 使用时工程中引入 io.github.openfeign:feign-micrometer 依赖即可，组件会自动生效
    // <dependency>
    //    <groupId>io.github.openfeign</groupId>
    //    <artifactId>feign-micrometer</artifactId>
    // </dependency>

    /**
     * 官方组件在低版本中无法自动生效，且开启的监控指标过多，有潜在的性能风险
     *
     * @param registry 非空
     * @return
     */
    @Bean
    public FeignClientBeanPostProcessor feignClientBeanPostProcessor(MeterRegistry registry) {
        return new FeignClientBeanPostProcessor(registry);
    }
}
